package cn.xuetian.mqtest.config;

import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 测试发现，只要mq server正常，该配的vhost，exchange，queue等配置好了，配置文件里写好了。这个文件里注释的这些都不用取消注释即可成功使用mq。
 * 截至当前状态。
 */
//连接rabbitMQ的基本配置
@Configuration
@EnableRabbit
public class RabbitConfig {

    @Value("${spring.rabbitmq.host}")
    private String host;

    @Value("${spring.rabbitmq.username}")
    private String username;

    @Value("${spring.rabbitmq.password}")
    private String password;

    @Value("${spring.rabbitmq.port}")
    private Integer port;


    @Value("${spring.rabbitmq.virtual-host}")
    private String vHost;


    @Value("${demo.mymqTest.exchange}")
    private String exchange;

    @Value("${demo.mymqTest.queue}")
    private String queue;

    @Value("${demo.mymqTest.routing}")
    private String routing;


//    @Bean
//    public ConnectionFactory connectionFactory() {
//        CachingConnectionFactory connectionFactory = new CachingConnectionFactory(host);
//        connectionFactory.setUsername(username);
//        connectionFactory.setPassword(password);
//        connectionFactory.setPort(port);
//        connectionFactory.setVirtualHost(vHost);
//        return connectionFactory;
//    }

//    @Bean
//    public AmqpAdmin amqpAdmin() {
//        return new RabbitAdmin(connectionFactory());
//    }

//    @Bean
//    public RabbitTemplate rabbitTemplate() {
//        return new RabbitTemplate(connectionFactory());
//    }

    //配置消费者监听的容器
//    @Bean
//    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory() {
//        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
//        factory.setConnectionFactory(connectionFactory());
//        factory.setConcurrentConsumers(3);
//        factory.setMaxConcurrentConsumers(10);
//        //factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);//设置确认模式手工确认 ???
//        return factory;
//    }


//    @Bean
//    public Queue demoTestQueue() {
//        return new Queue(queue);
//    }
//
//    @Bean
//    DirectExchange demoTestExchange() {
//        return new DirectExchange(exchange);
//    }
//
//    @Bean
//    Binding cacheRefreshBindingExchange(Queue demoTestQueue, DirectExchange demoTestExchange) {
//        return BindingBuilder.bind(demoTestQueue).to(demoTestExchange).with(routing);
//    }
}